/*builder-open key="local|full"*/
function addToLocalCache(cache,state) {
	if (cache) {
        if (!cache.checkXmlDoc()) {
            cache.setXmlDoc(state.xmlDoc);
        }
        if (!cache.checkXslDoc()) {
            cache.setXslDoc(state.xslDoc);
        }
        if (!cache.checkResultDoc()) {
            cache.setResultDoc(state.resultDoc);
        }
		cache.unlock();
	}
}
/*builder-close*/

/*builder-open key="base|full"*/
function addToBaseCache(caching,state) {
	if (caching  && caching.xml) {
		if (cacheXMLKey!='') {
			var cacheXMLValue = state.xmlDoc;
			innerCache.setElement(cacheXMLKey,cacheXMLValue);
		}
	}
	if (caching  && caching.xsl) {
		if (cacheXSLKey!='') {
			var cacheXSLValue = state.xslDoc;
			innerCache.setElement(cacheXSLKey,cacheXSLValue);
		}
	}
	if (caching && caching.result) {
		if (cacheResultKey!='') {
			var cacheResultValue = state.resultDoc;
			innerCache.setElement(cacheResultKey,cacheResultValue);
		}
	}
}
/*builder-close*/


/*builder-open key="ajax|full"*/
function addToAjaxCache(xhr,caching,cacheKey,value) {
	if (caching && caching.ajax) {
		if (cacheKey!='') {
			
			function getHeader(xhr,header) {
				var value = xhr.getResponseHeader(header);
				if (value) {
					return value;
				}
				return null;
			}
			
			function canCache(cachedValue) {
				var result = 0;
				if (cachedValue.cache) {
					if (cachedValue.cache.indexOf("no-cache")<0 &&
						cachedValue.cache.indexOf("no-store")<0) {
						if (cachedValue.cache.indexOf("max-age")>0) {
							var time = cachedValue.cache.match(/max-age\s*=\s*(\d*)?/i);
							if (time[1]) {
								date = new Date().getTime() + (time[1]*1000);
								if (cachedValue.expires) {
									cachedValue.expires = new Date(Math.min(cachedValue.expires.getTime(),date));
								} else {
									cachedValue.expires = new Date(date);
								}
								result = 100;
							} else {
								result = 200;
								cachedValue.cache = null;
							}
						} else {
							result = 100;
						}
					} else {
						result = 300;
						cachedValue.cache = null;
					}
				}
				if (cachedValue.expires) {
					if (cachedValue.expires > new Date()) {
						result = result+10;
					} else {
						cachedValue.expires = null;
					}
				}
				if (result != 0 && result != 200 && result < 300) {
					return true;
				}
				return false;
			}
			
			var cachedValue = {
				expires:null,
				cache:null,
				value:null
			};
			
			cachedValue.cache = getHeader(xhr,"Cache-control");
			if (!cachedValue.cache) {
				cachedValue.cache = getHeader(xhr,"Pragma");
			}
			var expiresHeader = getHeader(xhr,"Expires");
			if (expiresHeader) {
				cachedValue.expires = new Date(expiresHeader);
			}

			if (canCache(cachedValue)) {
				cachedValue.value = value;
				debug(cacheKey+" added to ajax cache!");
				netCache.setElement(cacheKey,cachedValue);
			}
		}
	}
}
/*builder-close*/

function call() {
	if (callback) {
		setTimeout(function(){
			callback();
		},10);
	}
}